package com.ocom.oauth.auth.sms;

import com.ocom.oauth.auth.CustomAbstractTokenGranter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 *
 *  说明：
 *    验证码
 *    定义验证码具体逻辑
 */
public class SmsTokenGranter extends CustomAbstractTokenGranter {

    private static final String SMS_AUTH = "sms_auth";

    private final SmsUserDetailService smsUserDetailService;

    public SmsTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, SmsUserDetailService smsUserDetailService) {
        super(tokenServices, clientDetailsService, requestFactory, SMS_AUTH);
        this.smsUserDetailService = smsUserDetailService;
    }

    @Override
    protected UserDetails getUserDetails(Map<String, String> parameters) {
        String phone = parameters.get("phone");
        String code = parameters.get("code");
        Long comId = Long.valueOf(parameters.get("comId"));
        String from = parameters.get("from");
        return smsUserDetailService.loadByNameAndCode(phone,code,comId,from);
    }
}
